package com.ruoyi.oar.api.util;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 百度身份证文字识别
 */
@Component
@Slf4j
public class BaiduAiIdCardUtil {
    private static String BAIDU_OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";

    @Value("${baidu.ocrUrl}")
    public void setRequestUrl(String ocrUrl) {
        BAIDU_OCR_URL = ocrUrl;
    }

    /**
     * 身份证解析
     * @param imgBase64Str
     * @return
     */
    public static Map<String, String> getIdCardInfo(String imgBase64Str){
        try{
            // 身份证图片Base64
            String accessToken = AuthServiceUtil.getAuth();
//            String filePath = "D:\\idcard.jpeg";
//            byte[] imgData = FileUtil.readFileByBytes(filePath);
//            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgBase64Str,"utf-8");
            String param = "id_card_side=" + "front" +
                    "&image=" + imgParam;
            String result = BaiduHttpUtil.post(BAIDU_OCR_URL,accessToken, param);
            Map<String, Object> data = JSON.parseObject(result, Map.class);
            if(data.get("error_code") != null){
                log.error("识别失败(错误代码："+data.get("error_code").toString()+")");
                throw new ServiceException("识别失败(错误代码："+data.get("error_code").toString()+")");
            }
            Map<String, Object> wordsResult = JSON.parseObject(data.get("words_result").toString(), Map.class);
            Map<String, String> addressMaP = (Map) wordsResult.get("住址");
            Map<String, String> csrqMap = (Map) wordsResult.get("出生");
            Map<String, String> xmMap = (Map) wordsResult.get("姓名");
            Map<String, String> gmsfzhMap = (Map) wordsResult.get("公民身份号码");
            Map<String, String> zjxbMap = (Map) wordsResult.get("性别");
            Map<String, String> zjmzMap = (Map) wordsResult.get("民族");
            String address = addressMaP == null ? "" : addressMaP.get("words");
            String csrq = csrqMap == null ? "" : csrqMap.get("words");
            String xm = xmMap == null ? "" : xmMap.get("words");
            String gmsfzh = gmsfzhMap == null ? "" : gmsfzhMap.get("words");
            String zjxb = zjxbMap == null ? "" : zjxbMap.get("words");
            String zjmz = zjmzMap == null ? "" : zjmzMap.get("words");
            // 返回数据
            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("address",address);
            resultMap.put("csrq",csrq);
            resultMap.put("xm",xm);
            resultMap.put("gmsfzh",gmsfzh);
            resultMap.put("zjxb",zjxb);
            resultMap.put("zjmz",zjmz);
            return resultMap;
        } catch (Exception e) {
            log.error("解析身份证失败"+e.getMessage());
        }
        return  null;
    }

}
